<?php
declare (strict_types = 1);

namespace app\admin\model\system;

use bbweb\basic\BaseModel;
use bbweb\traits\ModelTrait;

/**
 * 配置模型
 */
class SystemConfig extends BaseModel
{
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'system_config';

    use ModelTrait;

    /**
     * @param $menu
     * @return false|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getConfigValue($menu)
    {
        if (empty($menu) || !($value = self::where('menu_name', $menu)->value('value'))) return false;
        return $value;
    }

    /**
     * 获得多个参数
     * @param $menus
     * @return array
     */
    public static function getMore($menus)
    {
        $menus = is_array($menus) ? implode(',', $menus) : $menus;
        $list = self::where('menu_name', 'IN', $menus)->column('value', 'menu_name') ?: [];
        return $list;
    }


    /**
     * @return array
     */
    public static function getAllConfig()
    {
        $list = self::column('value', 'menu_name') ?: [];
        return $list;
    }


    /**
     * @param $filed
     * @param $value
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getOneConfig($filed, $value)
    {
        $where[$filed] = $value;
        return self::where($where)->find();
    }

    /**
     * @param $type
     * @return array
     */
    public static function getAll($type)
    {
        $where['type'] = $type;
        return self::where($where)->column('value', 'menu_name');
    }
}